React = require('react')
{connect} = require('react-redux')
{View,StyleSheet,InteractionManager,Text,Image,TextInput,TouchableOpacity} = require('react-native')
Button = require('antd-mobile/lib/button')
Toast = require('antd-mobile/lib/toast')
ImagePicker = require('antd-mobile/lib/image-picker')
Dimensions = require('Dimensions')
{ width, height } = Dimensions.get('window')
backImg = require('../img/back.png')
{modifyListItem} = require('../actions/page_scdzqz.js')

loadImage = (url)->


uploadImage = (file,planid,pilotCode,list,changeStateFunc,modifyListItem)->
  formData = new FormData()
  file =
    uri:file.url
    type:'multipart/form-data'
    name:'a.jpg'

  formData.append('files',file)
  formData.append('planid',planid)
  formData.append('pilotCode',pilotCode)
  uri = 'http://202.136.214.173/Home/FileUpload'
  fetch(uri,{
    method:'POST',
    headers:{
      'Content-Type':'multipart/form-data'
    }
    body:formData
  })
  .then (response)->
    debugger
    response.text()
  .then (responseData)->
    debugger
    obj = JSON.parse(responseData)
    console.log('responseData',responseData)
    #查找并更新list
    for i in list
      if i.CHDYNPLANID is planid
        i.URL = obj.url
    debugger
    modifyListItem()
    Toast.success '图片上传成功',->
      changeStateFunc()
  .catch (error)->
    debugger
    console.log(error)
    Toast.fail '图片上传失败，请稍后重试'



Page_scdzqz = React.createClass
  getInitialState:->
    # @props.refreshStart()
    if @props.data.URL
      showPic:true
      files:[]
    else
      showPic:false
      files:[]
  onChange:(files, type, index)->
    this.setState files:[files[files.length-1]]
  upload:->
    debugger
    that = @
    return if @state.files.length is 0
    file = @state.files[0]
    uploadImage(file,
      @props.data.CHDYNPLANID,
      @props.pilotCode,@props.list,
      ->that.setState showPic:true,
      @props.modifyListItem)
  navBack:->
    @props.navigator.pop()
  # fetch:->
  #   debugger
  #   @props.refreshList(@props.userInfo.token)
  render:->

      <View style={{height:height,width:width,backgroundColor:'#ffffff'}}>
        <View style={{flexDirection:'row',flex:0,height:60,alignItems:'center', justifyContent:'center',backgroundColor:'#0066FF'}}>
          <View style={{flex:1,padding:10}}>
            <TouchableOpacity onPress={@navBack}>
              <Image source={backImg}  style={{height:30,width:30,resizeMode:'contain'}}/>
            </TouchableOpacity>
          </View>
          <View style={{flex:2,justifyContent:'center',alignItems:'center'}}>
            <Text style={{fontSize:20,color:'#ffffff'}}>上传电子签证</Text>
          </View>
          <View style={{flex:1,alignItems:'flex-end',padding:10}}>
          </View>
        </View>
        {
          if @state.showPic
            <View style={{marginTop:10}}>
              <Image
                style={{width:width,height:height-200}}
                source={{uri: @props.data.URL}}
              />
              <View style={{margin:10}}>
                <Button type='primary' onClick={=>@setState showPic:false}>重新上传照片</Button>
              </View>
            </View>
          else
            [<View style={{margin:10}}>
              <ImagePicker
                files={@state.files}
                onChange={@onChange}
              />
            </View>
            <View style={{margin:10}}>
              <Button type='primary' onClick={@upload}>上传照片</Button>
            </View>]
        }

      </View>

# <Button type='primary' onClick={@fetch}>fetch</Button>

styles = StyleSheet.create
  image:
    width:width
    height:height
mapStateToProps = (state)->
  list:state.page_scdzqz.list
  userInfo:state.userInfo
  version:state.appState.version
  list:state.page_scdzqz.list
  isError:state.page_scdzqz.isError
  userInfo:state.userInfo
mapDispatchToProps = (dispatch,wonProps)->
  modifyListItem:()->
    dispatch modifyListItem()
module.exports = connect(mapStateToProps,mapDispatchToProps)(Page_scdzqz)
